<template>
  <account-wrap :tips="tips" :submit-text="submitText" :submitHandle="submitHandle" class="register-box">
    <el-row>
      <el-col :span="4" :offset="2">
        <el-upload
        :headers="uploadHeaders"
        class="avatar-uploader"
        action="/api/media/upload"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload">
          <img v-if="form.avatar" :src="form.avatar" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-col>
      <el-col :span="14" :offset="2">
        <el-form :model="form" :rules="rules" ref="login">
          <el-form-item prop="avatar" class="no-border"></el-form-item>
          <el-form-item prop="nickname">
            <el-row>
              <el-col :span="4"><i class="material-icons">person</i></el-col>
              <el-col :span="20">
                <el-input type="text" v-model="form.nickname" placeholder="输入昵称"></el-input>
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item prop="email">
            <el-row>
              <el-col :span="4"><i class="material-icons">email</i></el-col>
              <el-col :span="20">
                <el-input type="email" v-model="form.email" placeholder="输入邮箱"></el-input>
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item prop="camera">
            <el-row>
              <el-col :span="4"><i class="material-icons">photo_camera</i></el-col>
              <el-col :span="20">
                <el-input type="camera" v-model="form.camera" placeholder="输入相机型号"></el-input>
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item prop="password">
            <el-row>
              <el-col :span="4"><i class="material-icons">lock_outline</i></el-col>
              <el-col :span="20">
                <el-input type="password" v-model="form.password" placeholder="输入密码"></el-input>
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item prop="repassword">
            <el-row>
              <el-col :span="4"><i class="material-icons">lock</i></el-col>
              <el-col :span="20">
                <el-input type="password" v-model="form.repassword" placeholder="重新确认密码"></el-input>
              </el-col>
            </el-row>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </account-wrap>
</template>

<script>
import accountWrap from './conWrap';

export default {
  name: 'Register',
  data() {
    return {
      tips: {
        content: '点击进行登录',
        icon: 'person',
        link: '/login',
      },
      submitText: '注册',
      form: {
        email: '',
        avatar: '',
      },
      rules: {
        nickname: [{
          required: true,
          message: '请输入昵称，2-16字符',
          trigger: 'blur',
          min: 2,
          max: 16,
        }],
        email: [{
          type: 'email',
          required: true,
          message: '请输入正确邮箱',
          trigger: 'blur',
        }],
        camera: [{
          type: 'string',
          required: true,
          message: '请输入相机型号',
          trigger: 'blur',
        }],
        avatar: [{
          type: 'string',
          required: true,
          message: '请上传头像',
          trigger: 'blur',
        }],
        password: [{
          required: true,
          message: '请输入密码，6-20字符',
          min: 6,
          max: 20,
          trigger: 'blur',
        }],
        repassword: [{
          required: true,
          validator: (rule, value, callback) => {
            if (this.form.password === value) {
              callback();
            } else {
              callback(new Error('两次密码不相同'));
            }
          },
        }],
      },
    };
  },
  computed: {
    uploadHeaders() {
      return {
        'x-csrf-token': this.$store.state.glob.csrf,
      };
    },
  },
  methods: {
    submitHandle() {
      this.$refs.login.validate((valid) => {
        if (valid) {
          const { nickname, email, camera, password, avatar } = this.form;
          this.$http.post('/user/register', {
            nickname, email, camera, password, avatar,
          })
            .then(() => {
              this.$notify({
                title: '成功',
                message: '注册成功',
                type: 'success',
              });
              this.$alert('注册成功，请在邮箱中对账号进行激活', '注册成功', {
                confirmButtonText: '确定',
                callback: () => {
                  this.$router.push('/login');
                },
              });
            });
        } else {
          return false;
        }
      });
    },
    handleAvatarSuccess(res) {
      this.form.avatar = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = ['image/jpeg', 'image/png', 'image/gif'].includes(file.type);
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 png/jpg/gif 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
  },
  components: {
    accountWrap,
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="sass">
@import "./style.scss";
</style>
<style>
  .no-border{
    border: none !important;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    width: 80px;
    width: 80px;
    line-height: 80px;
    margin-top: 30px;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    text-align: center;
    display: inline-block;
    margin-top: calc(50% - 14px);
  }
  .avatar {
    width: 80px;
    height: 80px;
    display: block;
  }
</style>

